#include "SoundManager.h"

#include <SDL_mixer.h>
SoundManager::SoundManager()
{
	audio_rate = 22050;
	audio_format = MIX_DEFAULT_FORMAT;
	audio_channels = 2;
	audio_buffers = 4096;
}

bool SoundManager::LoadSound(const std::string& filename)
{
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
	{
		return false;
	}
	m_sounds[filename] = Mix_LoadWAV(filename.c_str());
	if (m_sounds[filename] == NULL)
	{
		std::cout << "sound: " + filename + " failed to load" << std::endl;
		return false;
	}

	std::cout << "sound: " + filename << " loaded" << std::endl;
	return true;
}

bool SoundManager::LoadMusic(const std::string& filename)
{
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
	{
		return false;
	}
	m_musics[filename] = Mix_LoadWAV(filename.c_str());
	if (m_musics[filename] == NULL)
	{
		return false;
	}
	return true;
}

bool SoundManager::PlaySound(const std::string& filename, int loops)
{
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
	{
		return true;
	}	
	if (m_sounds[filename] == NULL)
	{
		std::cout << "Failed to play: " + filename << std::endl;
	}
	Mix_PlayChannel(-1, m_sounds[filename], loops);

	return false;
}

bool SoundManager::PlayMusic(const std::string& filename, int loops)
{	
	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
	{
		return true;
	}
	
	if (m_musics[filename] == NULL)
	{
		std::cout << "file: " + filename + " was not loaded properly" << std::endl;
	}

	Mix_PlayChannel(-1, m_musics[filename], loops);

	return false;
}